"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : Binary
Case Name   : 数据库兼容模式为A时不支持Binary类型
Create At   : 2023/10/7
Owner       : @li-xin12345
Description :
    1、创建数据库（兼容模式为A）
    2、使用步骤1创建的数据库，建表含binary类型
    3、清理环境
Expect      :
    1、成功
    2、合理报错
    3、成功
History     :
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Testbinary(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = 'db_binary_0014'
        self.tb_name = 't_binary_0014'
        self.error_msg = 'ERROR:  type "binary" does not exist'

    def test_binary(self):
        text = '-----step1:创建数据库（兼容模式为A）;expect:成功-----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} " \
                  f"dbcompatibility ='A';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd, dbname='postgres')
        self.log.info(sql_res)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, sql_res,
                      '执行失败' + text)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res,
                      '执行失败' + text)

        text = '-----step2:使用步骤1创建的数据库，建表含binary类型;' \
               'expect:合理报错-----'
        self.log.info(text)
        sql_res = self.sh_primary.execut_db_sql(
            f"drop table if exists {self.tb_name};"
            f"create table {self.tb_name}(v1 binary(6));", dbname=self.db_name)
        self.log.info(sql_res)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, sql_res,
                      '执行失败：' + text)
        self.assertIn(self.error_msg, sql_res, '执行失败：' + text)

    def tearDown(self):
        self.log.info('-----step3:清理环境; expect:成功-----')
        sql_res = self.sh_primary.execut_db_sql(
            f"drop database if exists {self.db_name};", dbname='postgres')
        self.log.info(sql_res)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, sql_res,
                      "删库失败")
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
